package musicstore.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import musicstore.dao.AlbumDao;
import musicstore.entity.Album;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class AlbumDaoImpl extends HibernateDaoSupport implements AlbumDao {

	public List<Album> getAlbumsByDateReleased() {
		return getHibernateTemplate().execute(new HibernateCallback<List<Album>>() {

			@Override
			public List<Album> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(Album.class);
				cri.addOrder(Order.desc("dateReleased"));
				return cri.setFirstResult(0).setMaxResults(9).list();
			}
		});
		
	}
	
	@Override
	public List<Album> getListByGenreId(final int genreId,final int pageNum,final int pageSize) {
		return getHibernateTemplate().execute(new HibernateCallback<List<Album>>() {

			@Override
			public List<Album> doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql = "from Album a where 1=1 ";
				Map<String,Object> list = new HashMap<String, Object>();
				if(genreId > 0){
					hql += "and a.genre.id = :genreId";
					list.put("genreId", genreId);
				}
				if(pageNum > 0 && pageSize > 0){
					return session.createQuery(hql).setProperties(list)
							.setFirstResult((pageNum-1)*pageSize)
							.setMaxResults(pageSize).list();
				}
				return session.createQuery(hql)
						.setProperties(list)
						.list();
			}
		});
	}

	@Override
	public Album getAlbumById(int id) {
		return (Album)getHibernateTemplate().get(Album.class, id);
	}

	@Override
	public List<Album> getListByGenreIdAndTitle(final String title, final int genreId,
			final int pageNum, final int pageSize) {
		return getHibernateTemplate().execute(new HibernateCallback<List<Album>>() {

			@Override
			public List<Album> doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql = "from Album a where 1=1 ";
				Map<String,Object> list = new HashMap<String, Object>();
				if(genreId > 0){
					hql += "and a.genre.id = :genreId";
					list.put("genreId", genreId);
				}
				if(title!= null &&!title.isEmpty()){
					hql+=" and title like :title";
					list.put("title", "%"+title+"%");
				}
				if(pageNum > 0 && pageSize > 0){
					return session.createQuery(hql).setProperties(list)
							.setFirstResult((pageNum-1)*pageSize)
							.setMaxResults(pageSize).list();
				}
				return session.createQuery(hql)
						.setProperties(list)
						.list();
			}
		});
	}

	@Override
	public void update(Album album) {
		getHibernateTemplate().update(album);
	}

	@Override
	public List<String> getTitleByPrefix(final String title, final int pageSize) {
		
		return getHibernateTemplate().execute(new HibernateCallback<List<String>>() {

			@Override
			public List<String> doInHibernate(Session session)
					throws HibernateException, SQLException {
				List<String> list = new ArrayList<String>();
				List<Album> albums = session.createQuery("from Album where title like ?")
										.setString(0, title+"%")
										.setMaxResults(pageSize)
										.list();
				for(Album a : albums){
					list.add(a.getTitle());
				}
				return list;
			}
		});
		
	}
	

	@Override
	public void add(Album album) {
		getHibernateTemplate().save(album);
	}

}
